package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api("套餐管理")
@RequestMapping("/admin/setmeal")
public class SetmealController {


    @Autowired
    private SetmealService setmealService;


    @PostMapping
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @GetMapping("page")
    public Result<PageResult> list(SetmealPageQueryDTO setmealPageQueryDTO) {
        return Result.success(setmealService.list(setmealPageQueryDTO));
    }
    @GetMapping("{id}")
    public Result getById(@PathVariable("id") Long id) {
        return Result.success( setmealService.getById(id));
    }
    @PutMapping
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result updateStatus(Long id, @PathVariable("status") Integer status) {
        setmealService.updateStatus(id, status);
        return Result.success();
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result delete(@RequestParam("ids") String ids) {
        //ids以逗号分隔
        List<Long> idList = new java.util.ArrayList<>();
        for (String id : ids.split(",")) {
            idList.add(Long.parseLong(id));
        }
        setmealService.delete(idList);
        return Result.success();
    }
}
